<template>
  <div>
    <!-- 收件人信息维护 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTable
        ref="table"
        columnsRef="qjhAddresseeInfoMaintian"
        :columns="columns"
        :isSelectColumn="true"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :datakey="'records'"
        :onColumnsChange="columns => (this.columns = columns)"
        :onRowSelectChange="syncSelect"
      >
        <template slot="controls" slot-scope="props">
          <el-button size="small" type="primary" @click="addHandle">新 增</el-button>
          <el-button size="small" type="primary" @click="deleteHandle">删 除</el-button>
          <multiuse-button size="small" type="primary" :click="exportHandle">导 出</multiuse-button>
        </template>
      </FilterTable>
    </Spin>

    <BaseDialog :visible.sync="visible" :title="drawerTitle" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <SonModel :type="handleType" :rowData="formData" @close="emitCloseHandler" />
    </BaseDialog>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils/index';
import { queryList, exportExcel, deleteByList,exporturl } from '@/api/omFinance/InvoiceMailManage/AddresseeInfoMaintian';
import SonModel from './SonModel';

export default {
  name: 'AddresseeInfoMaintian',
  mixins: [dictionary],

  components: {
    SonModel
  },
  data() {
    this.BaseTable = null;
    this.conf = { add: '新增', edit: '编辑', show: '明细' };

    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: queryList,
      fetchParams: {},
      formData: {},
      exportLoading: false,
      visible: false,
      selectRows: [],
      handleType: '',
      drawerTitle: ''
    };
  },

  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    addHandle() {
      this.handleType = 'add';
      this.formData = {};
      this.showHandler();
    },
    editHandle(row) {
      this.handleType = 'edit';
      this.formData = row;
      this.showHandler();
    },
    async deleteHandle() {
      if (this.selectRows && this.selectRows.length > 0) {
        let res = await deleteByList(this.selectRows);
        console.log('res::', res);
        if (res.resultCode == '200') {
          this.$notify.success({ title: '提示信息', message: '操作成功' });
          //刷新页面
          this.fetchParams = { ...this.fetchParams };
        }
      } else {
        this.$message({
          type: 'info',
          message: '请选择数据'
        });
      }
    },
    syncSelect(rows) {
      console.log('rows:', rows);
      this.selectRows = rows;
    },
    showHandler() {
      this.drawerTitle = `${this.conf[this.handleType]}`;
      this.visible = true;
    },
    emitCloseHandler(val, type) {
      this.visible = val;
      if (type === 'refresh') {
        // 重新加载
        this.fetchParams = { ...this.fetchParams };
      }
    },

    // 导出
    async exportHandle() {
        let json = Object.assign({}, { CODE: 'CODE_0000096' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // await this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     this.exportLoading = true;

      //     exportExcel(this.fetchParams)
      //       .then(response => {
      //         exportExcelFile(response.data, '收件人信息维护');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    createdFrom() {
      return [
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'SELECT',
          label: '业务类型',
          fieldName: 'businessType',
          placeholder: '请选择',
          itemList: this.createDictList('6089')
        },
        {
          type: 'INPUT',
          label: '收件人姓名',
          fieldName: 'recipientName',
          placeholder: '请输入'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 50,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: props => {
            return <span>{this.createDictText(props.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '业务类型',
          dataIndex: 'businessType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6089'),
          render: props => {
            return <span>{this.createDictText(props.row.businessType, '6089')}</span>;
          }
        },
        {
          title: '收件人姓名',
          dataIndex: 'recipientName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '收件人邮编',
          dataIndex: 'recipientPostCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '收件人电话',
          dataIndex: 'recipientPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '收件人移动电话',
          dataIndex: 'recipientMobile',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '所在省',
          dataIndex: 'provinceId',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createRegionsList(),
          render: p => {
            let arr = this.createRegionsList();
            let str = '';
            arr.map(x => {
              if (x.value == p.row.provinceId) {
                str = x.text;
              }
            });
            return <span>{str}</span>;
          }
        },
        {
          title: '所在市',
          dataIndex: 'cityId',
          sorter: true,
          render: p => {
            let arr = this.createcityList(p.row.provinceId);
            let str = '';
            arr.map(x => {
              if (x.value == p.row.cityId) {
                str = x.text;
              }
            });
            return <span>{str}</span>;
          }
        },
        {
          title: '所在区（县）',
          dataIndex: 'districtId',
          sorter: true,
          render: p => {
            let arr = this.district(p.row.cityId);
            let str = '';
            arr.map(x => {
              if (x.value == p.row.districtId) {
                str = x.text;
              }
            });
            return <span>{str}</span>;
          }
        },
        {
          title: '详细地址',
          dataIndex: 'address',
          sorter: true,
          filter: true,
          filterType: 'input'
        }
      ];
    }
  }
};
</script>
